package com.thinkgem.jeesite.modules.sys.service;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.modules.sys.dao.ShopAddDao;
import com.thinkgem.jeesite.modules.sys.dao.TbCommunityMapper;
import com.thinkgem.jeesite.modules.sys.entity.TbCommunity;
import com.thinkgem.jeesite.modules.sys.entity.TbCommunityExample;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by jyy on 2016/10/17 0017.
 */
@Service
public class TbCommunityService {
    @Autowired
    private TbCommunityMapper tbCommunityMapper;
    @Autowired
    private ShopAddDao shopAddDao;
    //代理商下的小区的信息列表
    public Page<TbCommunity> findByPage(Page<TbCommunity> tbCommunityPage, TbCommunity tbCommunity){
        //得到代理商所在的省市
        User users=shopAddDao.userAreas(tbCommunity.getUid());
        //根据代理商的省市查到他所管理的小区
        if(users!=null){
            tbCommunity.setProvinceId(Integer.parseInt(users.getProvinces()));
            tbCommunity.setCityId(Integer.parseInt(users.getCitys()));
        }
        tbCommunity.setPage(tbCommunityPage);
        List list = tbCommunityMapper.findByPage(tbCommunity);
        tbCommunityPage.setList(list);
        return tbCommunityPage;
    }
    public Page<TbCommunity> allCommunity(Page<TbCommunity> tbCommunityPage, TbCommunity tbCommunity){
        tbCommunity.setPage(tbCommunityPage);
        List list = tbCommunityMapper.allCommunity(tbCommunity);
        tbCommunityPage.setList(list);
        return tbCommunityPage;
    }
    public int delete(TbCommunity tbCommunity) {
        return tbCommunityMapper.deleteCommunity(tbCommunity.getId());
    }

    public int saveOrUpdateAdvertise(TbCommunity tbCommunity) {
        int result =0 ;
        if(null!=tbCommunity){
            TbCommunity pro=tbCommunityMapper.proAndCity(UserUtils.getUser().getId());
            tbCommunity.setCityId(pro.getCityId());
            tbCommunity.setCityName(pro.getCityName());
            tbCommunity.setProvinceId(pro.getProvinceId());
            tbCommunity.setProvinceName(pro.getProvinceName());
            TbCommunity ares=tbCommunityMapper.ares(tbCommunity.getAreaId()+"");
            tbCommunity.setAreaId(ares.getAreaId());
            tbCommunity.setAreaName(ares.getAreaName());
            tbCommunity.setUpdateTime(new Date());
            if(null!=tbCommunity.getId()){
                result = tbCommunityMapper.updateByPrimaryKeySelective(tbCommunity);
            }else{
                tbCommunity.setCreateTime(new Date());
                result = tbCommunityMapper.insertSelective(tbCommunity);
            }
        }
        return result;
    }
    public TbCommunity ares(String areId){
        return tbCommunityMapper.ares(areId);
    }
    public TbCommunity get(Integer id) {
        return tbCommunityMapper.selectByPrimaryKey(id);
    }
    //根据省市和小区名验证是否唯一
    public TbCommunity getCommitByCommName(String commName){
        TbCommunity tbCommunity=new TbCommunity();
        tbCommunity.setCommName(commName);
        User user=shopAddDao.userAreas(UserUtils.getUser().getId());
        tbCommunity.setProvinceId(Integer.parseInt(user.getProvinces()));
        tbCommunity.setCityId(Integer.parseInt(user.getCitys()));
        TbCommunity community= tbCommunityMapper.getCommitByCommName(tbCommunity);
        return community;
    }
    //根据省市区和小区名称验证是否唯一
    public TbCommunity agentCheckCommName(String commName,Integer aes){
        TbCommunity tbCommunity=new TbCommunity();
        tbCommunity.setCommName(commName);
        User user=shopAddDao.userAreas(UserUtils.getUser().getId());
        tbCommunity.setProvinceId(Integer.parseInt(user.getProvinces()));
        tbCommunity.setCityId(Integer.parseInt(user.getCitys()));
        tbCommunity.setAreaId(aes);
        TbCommunity community= tbCommunityMapper.agentCheckCommName(tbCommunity);
        return community;
    }
}
